//
//  DeviceUtil.swift
//  Extend
//
//  Created by 元穗 on 2017/5/22.
//  Copyright © 2017年 yuansui. All rights reserved.
//

import Foundation
import AVFoundation
import Alamofire

public enum DeviceLauguage {
    case Chinese_T 
    case Chinese_S
    case English
}

public class DeviceUtil {
    
    /// 判断摄像头权限
    ///
    /// - Parameter limitBlock: 无权限时的操作
    public class func isOpenCamera(limitBlock: @escaping () ->(), allowBlock: @escaping () ->()) {
        var b = false
        
        switch AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) {
        case AVAuthorizationStatus.authorized://已经授权
            b = true
            break
        case AVAuthorizationStatus.notDetermined:
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
                if(granted){
                    //点击确定
                    allowBlock()
                    return
                } else {
                    //点击了不允许
                    limitBlock()
                    b = false
                    return
                }
            })
            break
        case AVAuthorizationStatus.denied:
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
                if(!granted){
                    //否认
                } else {
                    b = true
                }
            })
            break
        default:
            break
        }
        
        if b == false {
            //没有权限
            limitBlock()
        } else {
            allowBlock()
        }
    }
    
    
    /// 麦克风权限
    ///
    /// - Parameters:
    ///   - limitBlock: 允许执行
    ///   - allowBlock: 不允许执行
    public class func isOpenMic(limitBlock: @escaping () ->(), allowBlock: @escaping () ->()) {
        var b = false
        
        switch AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeAudio) {
        case AVAuthorizationStatus.authorized://已经授权
            b = true
            break
        case AVAuthorizationStatus.notDetermined:
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (granted: Bool) -> Void in
                if(granted){
                    //点击确定
                    allowBlock()
                } else {
                    //点击了不允许
                    limitBlock()
                    b = false
                }
            })
            break
        case AVAuthorizationStatus.denied:
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (granted: Bool) -> Void in
                if(!granted){
                    //否认
                } else {
                    b = true
                }
            })
            break
        default:
            break
        }

        if b == false {
            //没有麦克风权限
            limitBlock()
        } else {
            allowBlock()
        }
    }
    
    ///请求获取麦克风权限
    public class func getMicAuthor() {
        //判断当前是否有麦克风权限
        AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: {(_ granted: Bool) -> Void in
            print("\(granted ? "麦克风准许" : "麦克风不准许")")
        })
    }
    
    /// 系统版本号是否高于该版本
    ///
    /// - Parameter version: 版本号
    /// - Returns: Bool
    public class func overVersion(version: Int) -> Bool {
        return ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: version, minorVersion: 0, patchVersion: 0))
    }
    
    ///判断当前设备是否是iPhone X
    public class func isIphoneX() -> Bool {
        if UIScreen.main.bounds.height == 812 {
            return true
        }
        return false
    }
    
    ///判断当前设备是否是iphone5及一下
    public class func isIphone5() -> Bool {
        if UIScreen.main.bounds.width == 320 {
            return true
        }
        return false
    }
    
    //返回设备宽度比例
    public class func getDeviceScale() -> CGFloat {
        return UIScreen.main.bounds.height / 667
    }
}
